﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class VA03VK03SUMData : BaseFunction
    {
        public bool flagRegenerateLastMonthData;
        ArrayList ListTris = new ArrayList();

        List<WGSummary> ListWGSummary;
        string TrisInclude = "";
        DataTable tbAB50OUTPUT;
        public int ThreadCount;
        public VA03VK03SUMData()
            : base()
        {
            tbAB50OUTPUT = new DataTable("AB50OUTPUT");
            tbAB50OUTPUT.Columns.Add("SUMYEAR", typeof(string));
            tbAB50OUTPUT.Columns.Add("SUMMONTH", typeof(string));
            tbAB50OUTPUT.Columns.Add("LSLIEFNR", typeof(string));
            tbAB50OUTPUT.Columns.Add("FACTORY", typeof(string));
            tbAB50OUTPUT.Columns.Add("BATCHES", typeof(int));
            tbAB50OUTPUT.Columns.Add("SUBQTY", typeof(double));
            tbAB50OUTPUT.Columns.Add("SUBYIELDQTY", typeof(double));
            tbAB50OUTPUT.Columns.Add("G10KGORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("OTDORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("GOTDORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("MAXDELAYDAYS", typeof(int));
            tbAB50OUTPUT.Columns.Add("LOTDORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("MAXLESSDAYS", typeof(int));
        }
        public VA03VK03SUMData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string VKPAUFNUM;
            string WBSRTDATE, WBENTDATE;
            WBSRTDATE = dwABPOS["WBSRTDATE"].ToString().Trim();
            WBENTDATE = dwABPOS["WBENTDATE"].ToString().Trim();

            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region
                or1.CommandText = "SELECT VKPLIEFTERM,KDKUNDNA,VKPAUFKRS||'-'||VKPAUFLFD AS VKORDERNUM"
                    + ",VKPPOSTYP,SUBSTR(VKPART,1,1) AS VKPARTTYPE,VKPNEUDAT,TRIM(VKPART)||'-'||TRIM(VKPFARBE) AS ARTICLE"
                + ",ROUND(TRUNC(DECODE(TRIM(VKPMEEH),'M',VKPMENGE1/0.9144,VKPMENGE1)/100),0) AS VKPMENGE1,VKPMEEH " 
                +" FROM VKPOS P LEFT JOIN KDSTAMM M ON P.VKPKUNDNR=KDKUNDNR"
                +" WHERE VKPSA<>'AJ'"
                + " AND VKPNEUDAT>='" + WBSRTDATE + "' AND VKPNEUDAT<='" + WBENTDATE + "' "
                + " ORDER BY 1,2,3,4";
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "VA03VK03SUM";
                    SetDefault(tableLocal);
                    tbAB50OUTPUT.Clear();
                    byte[] b;
                    string value;
                    string sTemp = "";
                    string Column = "";
                    string ARTICLE = "", WBBDPART, FACTORY;
                    int PCNUM;
                    double WBEWMTR;
                    DataRow dwOUTPUT;
                    List<DataRow> dltRows = new List<DataRow>();
                    ListWGSummary = new List<WGSummary>();
                    string SUMMONTH, SUMYEAR, LSLIEFNR;
                    SUMYEAR = WBSRTDATE.Substring(0, 4);
                    SUMMONTH = WBSRTDATE.Substring(4, 2);
                    int G10KGORDERS;
                    int dYear, dMonth, dDay, eYear, eMonth, eDay;
                    string ABPLIEFDAT, ABPERLDAT;
                    DateTime dDate, eDate;
                    bool hasdDate = false, haseDate = false;
                    bool flagnew = false;
                    string VKPARTTYPE, VKPPOSTYP;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "KDKUNDNA";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value; 
                        #endregion
                        Column = "ARTICLE";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion 

                        VKPARTTYPE = dwTemp["VKPARTTYPE"].ToString();
                        switch (VKPARTTYPE)
                        { 
                            case "1":
                                dwTemp["VKPARTTYPE"] = "市场";
                                break;
                            case"2":
                                dwTemp["VKPARTTYPE"] = "高档";
                                break;
                            case"3":
                            case"4":
                            default:
                                dwTemp["VKPARTTYPE"] = "色织";
                                break;
                        }
                        dwTemp["VKPMEEH"] = "码成";
                        VKPPOSTYP = dwTemp["VKPPOSTYP"].ToString().Trim();
                        switch (VKPPOSTYP)
                        { 
                            case"B":
                                  dwTemp["VKPPOSTYP"] = "大货";
                                break;
                            case"S": 
                            default:
                                  dwTemp["VKPPOSTYP"] = "试样";
                            break;
                        }  
                    } 
                    setLocal.AcceptChanges();
                    set4Return.Merge(setLocal); 
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        public DataSet BuildWBBST(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}